"""
/***************************************************************************
GridCreatorDialog
A QGIS plugin
Creates a new polygon grid layer.
                             -------------------
begin                : 2009-10-01 
copyright            : (C) 2009 by Mark Baas
email                : mark.baas123@gmail.com 
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from Ui_GridCreator import Ui_GridCreator

from qgis.core import *
from qgis.gui import *

# create the dialog for zoom to point
class GridCreatorDialog(QDialog):
    def __init__(self, cb_ok): 
        QDialog.__init__(self) 

        # Set up the user interface from Designer. 
        self.ui = Ui_GridCreator()
        self.ui.setupUi(self) 
        
        self.cb_ok = cb_ok

        # Load layer in to combox
        layermap = QgsMapLayerRegistry.instance().mapLayers()
        layerlist = []
        for name, layer in layermap.iteritems():
            layerlist.append(unicode(layer.name()))
        self.ui.sourceLayers.addItems(layerlist)

        # connect file dialog
        QObject.connect( self.ui.btnBrowse, SIGNAL( "clicked()" ), self.outFile )
        QObject.connect(self.ui.buttonBox, SIGNAL("accepted()"), self._accept)
        
    def _accept(self):
        self.setResult(1)
        self.cb_ok(self)
        self.hide()

    def outFile(self):
        settings = QSettings()
        dirName = settings.value( "/UI/lastShapefileDir" ).toString()
        filtering = QString( "Shapefiles (*.shp)" )
        encode = settings.value( "/UI/encoding" ).toString()
        fileDialog = QgsEncodingFileDialog( self, "Save output shapefile", dirName, filtering, encode )
        fileDialog.setDefaultSuffix( QString( "shp" ) )
        fileDialog.setFileMode( QFileDialog.AnyFile )
        fileDialog.setAcceptMode( QFileDialog.AcceptSave )
        fileDialog.setConfirmOverwrite( True )
        if not fileDialog.exec_() == QDialog.Accepted:
		    return None, None
        files = fileDialog.selectedFiles()
        settings.setValue("/UI/lastShapefileDir", QVariant( QFileInfo( unicode( files.first() ) ).absolutePath() ) )
        self.shapefileName, self.encoding = ( unicode( files.first() ), unicode( fileDialog.encoding() ) )
        self.ui.outputFilename.setText(self.shapefileName)

    def getLayer(self):
        myName = self.ui.sourceLayers.currentText()

        
        layermap = QgsMapLayerRegistry.instance().mapLayers()
        for name, layer in layermap.iteritems():
            if layer.type() == QgsMapLayer.VectorLayer and layer.name() == myName:
                if layer.isValid():
                    return layer
                else:
                    return None
                    
    def getOutput(self):
        return str(self.ui.outputFilename.text())
        
    def getSize(self):
        return float(self.ui.cellSize.value())
